package com.zgs.mall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMq
 */
@Configuration
public class MyMQConfig {
    /**
     * 死信队列 Queue Exchange Binding 都会自动创建（RabbitMq容器中没有）
     * 容器中
     * @return
     */
    @Bean
    public Queue stockDelayQueue(){
        /**
         * x-dead-letter-exchange:order-event-exchange 对列到期后信息到这个交换机
         * x-dead-letter-routing-key:order.release.order  死信路由
         * x-message-ttl:60000
         */
        Map<String, Object> arguments = new  HashMap<>();
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        arguments.put("x-dead-letter-routing-key","stock.release");
        arguments.put("x-message-ttl",1000*60*2);//过期时间 毫秒
       // String name,队列名字
        // boolean durable,是否持久化
        // boolean exclusive,是否排他
        // boolean autoDelete 是否自动删除
        Queue queue = new Queue("stock.delay.queue", true, false, false,arguments);
        return queue;
    }

    @Bean
    public Queue stockReleasStockQueue(){
        // String name,队列名字
        // boolean durable,是否持久化
        // boolean exclusive,是否排他
        // boolean autoDelete 是否自动删除
        Queue queue = new Queue("stock.release.stock.queue", true, false, false);
        return queue;
    }

    /**
     * 交换机
     * @return
     */
    @Bean
    public Exchange stockEventExchange(){
        TopicExchange topicExchange = new TopicExchange("stock-event-exchange", true, false);
        return topicExchange;
    }
    @Bean
    public Binding stockLockBinding(){
        //String destination,目的地-哪个队列/哪个交换机
        // DestinationType destinationType, //目的类 队列/交换机
        // String exchange,//交换机
        // String routingKey,//路由key
        //@Nullable Map<String, Object> arguments
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",null);
    }
    @Bean
    public Binding stockRelesOrderBinding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null);
    }
}
